/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sgt.onlinetest.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sgt.onlinetest.hibernatedao.HibernateDAOFactory;
import sgt.onlinetest.entity.User;
/**
 *
 * @author ASP1
 */
public class AddNewUser extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   public AddNewUser(){
       super();
   }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         HibernateDAOFactory factory = new HibernateDAOFactory();
         String btncreate = request.getParameter("btncreate");
         String username = request.getParameter("tbxusername");
         String password = request.getParameter("tbxpassword");
         String fullname = request.getParameter("tbxfullname");

         if (btncreate != null && username!=null && password!=null && fullname!=null) {
            User us = new User();
         us.setFullname(fullname);
         us.setPassword(password);
         us.setUsername(username);
         factory.getUserDAO().save(us);
         request.setAttribute("msg", "Sample data was saved successfully");
        }else
            request.setAttribute("msg", "Please check your input!");
         
         request.getRequestDispatcher("/WEB-INF/addnewuser.jsp").forward(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
